package com.check.controller;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.PageObject;
import com.check.pojo.RoResource;
import com.check.pojo.Role;
import com.check.service.RoleService;
import com.check.util.DateUtils;
import com.check.util.StringUtil;

@Controller
public class RoleController extends BaseController {
	
	@Autowired
	@Qualifier("roleService")
	private RoleService roleService;
	
	@RequestMapping("roleQuery.do")
	public ModelAndView roleQuery(HttpServletRequest request,HttpServletResponse response,
								  @ModelAttribute("pageObject") PageObject pageObject ) {
		if(pageObject.getCurrent() == 0){
			pageObject.setCurrent(1);
		}
		List<Role> roleList =roleService.findAllRole(pageObject);
		pageObject.setTotal(roleService.findRoleCount());
		pageObject.setUrl("roleQuery.do");
		saveSysLog(RE_ROLE,LOG_QUERY,request);
		return new ModelAndView("page/role/roleList")
		           .addObject("roleList", roleList)
		           .addObject("path", request.getContextPath())
		           .addObject("page", pageObject.toPage());
	}
	
	@RequestMapping("toAddRole.do")
	public ModelAndView toAddRole(HttpServletRequest request,HttpServletResponse response) {
		return new ModelAndView("page/role/roleAdd")
		           .addObject("path", request.getContextPath());
	}
	
	@RequestMapping(value = "addRole.do", method = RequestMethod.POST)
	public void addRole(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("role") Role role) {
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			role.setRoName(URLDecoder.decode(request.getParameter("roName"), "utf-8"));
			role.setRoId(StringUtil.createUUID());
			saveSysLog(RE_ROLE,LOG_ADD,request);
			if(roleService.addRole(role)){
				flag = "1";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	@RequestMapping("toUpdRole.do")
	public ModelAndView toUpdRole(HttpServletRequest request,HttpServletResponse response) {
		String roId = request.getParameter("roId");
		Role role = roleService.findRoleById(roId);
		return new ModelAndView("page/role/roleUpd")
		           .addObject("path", request.getContextPath())
		           .addObject("role", role);
	}
	
	@RequestMapping("updateRole.do")
	public void updateRole(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("role") Role role) {
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			role.setRoName(URLDecoder.decode(request.getParameter("roName"), "UTF-8"));
			saveSysLog(RE_ROLE,LOG_UPDATE,request);
			if(roleService.updateRole(role)){
				flag = "1";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	@RequestMapping("toSetRole.do")
	public ModelAndView toSetRole(HttpServletRequest request,HttpServletResponse response) {
		String roId = request.getParameter("roId");
		Role role = roleService.findRoleById(roId);
		String treeJson = roleService.getTreeJson(roId);
		return new ModelAndView("page/role/roleSetMenu")
		           .addObject("path", request.getContextPath())
		           .addObject("role", role)
		           .addObject("treeJson", treeJson);
	}
	@RequestMapping("setRole.do")
	public void setRole(HttpServletRequest request,HttpServletResponse response) {
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			String roId = request.getParameter("roId");
			roleService.deleteRoRe(roId);
			String[] rId = request.getParameterValues("rId");
			for(String temp: rId){
				RoResource roRe = new RoResource();
				roRe.setRsId(StringUtil.createUUID());
				roRe.setRoId(roId);
				roRe.setrId(Integer.parseInt(temp));
				roleService.addRoRe(roRe);
			}
			saveSysLog(RE_ROLEMENU, LOG_UPDATE, request);
			flag = "1";
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping("deleteRole.do")
	public void deleteRole(HttpServletRequest request,HttpServletResponse response) {
		String roId = request.getParameter("roId");
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			if(roleService.deleteRole(roId)){
				roleService.deleteRoRe(roId);
				flag = "1";
			}
			saveSysLog(RE_ROLE, LOG_DELETE, request);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
}
